
package com.CST.controller.cb;

import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;
import com.CST.entity.cb.BdDepartment;
import com.CST.entity.cb.RuleAmortization;
import com.CST.entity.cb.selectInfo;
import com.CST.service.cb.IRuleAmortizationService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p></p>
 *
 * @author: y
 * @create: 2021-11-03 14:27
 **/
@Api(tags = {"科室分摊"})
@RestController
@RequestMapping("/ruleAmortization")
public class RuleAmortizationController {

    @Autowired
    private IRuleAmortizationService targetService;

    @PostMapping("/getRuleAmortization")
    @ApiOperation("获取科室分摊")
    public ResultObject getRuleAmortization(@RequestBody RuleAmortization entity) {
        List<RuleAmortization> result = targetService.getRuleAmortization(entity);
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<RuleAmortization>());
        }
//        PageInfo<RuleAmortization> resultList = PageHelperTool.excutePageInfo(result, entity.getPageIndex(), entity.getPageSize());
        return ResultObject.successReturn(result);
    }

    @PostMapping("/getRuleAmortizationById")
    @ApiOperation("获取科室分摊ByID")
    public ResultObject getRuleAmortizationById(@RequestBody RuleAmortization entity) {
        RuleAmortization result = targetService.getRuleAmortizationById(entity);
        return ResultObject.successReturn(result);
    }


    @PostMapping("/getYearMonth")
    @ApiOperation("获取年月下拉数据")
    public ResultObject getYearMonth() {
        List<selectInfo> result = targetService.getYearMonth();
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<>());
        }
        return ResultObject.successReturn(result);
    }

    @GetMapping("/checkNoAmortizationDept")
    @ApiOperation("检查未配置规则科室")
    public ResultObject checkNoAmortizationDept(int year,int month) {
        List<BdDepartment> result = targetService.checkNoAmortizationDept(year,month);
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<>());
        }
        return ResultObject.successReturn(result);
    }



    @PostMapping("/addRuleAmortization")
    @ApiOperation("新增科室分摊")
    public ResultObject addRuleAmortization(@RequestBody RuleAmortization entity) {
        Response result = targetService.addRuleAmortization(entity);
        //s
        if (entity.getAmortizationCode() == null || entity.getDeptName() == null) {
            return ResultObject.errorReturn("添加的科室或者分摊类型不能为空!");
        }
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/insertBatchRuleAmortization")
    @ApiOperation("继承添加")
    public ResultObject insertBatchRuleAmortization(@RequestBody RuleAmortization entity) {
        Response result = targetService.insertBatchRuleAmortization(entity);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/modifyRuleAmortization")
    @ApiOperation("修改科室分摊")
    public ResultObject modifyRuleAmortization(@RequestBody RuleAmortization entity) {
        if (entity.getId() == null || entity.getId() == 0) {
            return ResultObject.errorReturn("未获取到记录ID!");
        }
        Response result = targetService.modifyRuleAmortization(entity);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/batchDeleteRuleAmortization")
    @ApiOperation("删除科室分摊")
    public ResultObject batchDeleteRuleAmortization(@RequestBody List<Integer> ids) {
        if (ids == null || ids.stream().count() == 0) {
            return ResultObject.errorReturn("请选择要删除的记录");
        }
        Response result = targetService.batchDeleteRuleAmortization(ids);
        return ResultObject.resultReturn(result);
    }

}